﻿<%@ Page Title="企划订单量" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet" />
    <link href="../assets/css/shelf/base.css" rel="stylesheet" />
    <link href="../assets/js/plugins/fancybox/jquery.fancybox.css" rel="stylesheet" />
    <link href="../assets/css/plugins/webuploader/webuploader.css" rel="stylesheet" />
    <link href="../assets/img/validate/validate.css" rel="stylesheet" />
    
<link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <style type="text/css">
        .module-info .control-label {
            float: left;
            text-align: right;
            min-width: 110px;
        }

        @media (min-width: 528px) {
            .modal-dialog {
                width: 508px;
                margin: 5px auto;
            }
        }

        .quantity-add {
            cursor: pointer;
        }

            .quantity-add.glyphicon, .quantity-remove.glyphicon {
                display: block;
                cursor: pointer;
            }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container"></div>
            <div class="condition">
                <label>店铺</label>
                <input id="search_keys" class="form-control input-w170" placeholder="店铺编号或名称" />
                <label>货号</label>
                <input id="search_materialID" class="form-control input-w170" placeholder="货号" />
            </div>
            <div class="right-content-box">
                <div class="ibox-content">
                    <table class="footable table table-stripped">
                        <thead></thead>
                        <tbody id="pageList"></tbody>
                    </table>
                </div>
            </div>

            <div class="pageControl"></div>
        </div>
    </div>

    <div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
        <form id="checkModuleForm" method="get" action="#">
            <div class="modal-dialog">
                <div class="modal-content animated bounceInRight">
                    <div class="modal-header">
                        <span class="editTitle">企划订单量</span>
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">&times;</span>
                            <span class="sr-only">关闭</span>
                        </button>
                    </div>
                    <div id="message" style="display: none"></div>
                    <div class="modal-body">
                        <table class="module-info" cellpadding="0" cellspacing="0">
                            <thead></thead>
                            <tbody>
                                <tr>
                                    <td colspan="2">
                                        <div class="form-group">
                                            <label class="control-label">公司代码</label>
                                            <input id="CompanyID" type="text" name="CompanyID" placeholder=""
                                                datacol="yes" err="" checkexpession="NotNull"
                                                maxlength="5" class="form-control input-w342" value="KM" readonly />
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <div class="form-group">
                                            <label class="control-label">店铺名称<span class="must-sign">*</span></label>
                                            <input id="shopSearch" name="shopSearch" maxlength="100"
                                                datacol="yes" err="店铺名称" checkexpession="NotNull"
                                                placeholder="请输入店铺名称或编号" class="form-control input-w342" />
                                            <input id="ShopName" name="ShopName" type="hidden" />
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <div class="form-group">
                                            <label class="control-label">店铺编号<span class="must-sign">*</span></label>
                                            <input id="ShopCode" type="text" name="ShopCode" placeholder=""
                                                datacol="yes" err="店铺编号" checkexpession="NotNull"
                                                maxlength="5" class="form-control input-w110" />
                                        </div>
                                    </td>
                                    <td>
                                        <div class="form-group">
                                            <label class="control-label">店铺内码<span class="must-sign">*</span></label>
                                            <input id="ShopID" type="text" name="ShopID" placeholder=""
                                                datacol="yes" err="店铺内码" checkexpession="NotNull"
                                                maxlength="5" class="form-control input-w110" readonly="" />
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <div class="form-group">
                                            <label class="control-label">货号<span class="must-sign">*</span></label>
                                            <input id="MaterialID" type="text" name="MaterialID" placeholder=""
                                                datacol="yes" err="货架数量" checkexpession="NotNull"
                                                maxlength="5" class="form-control input-w342" />
                                        </div>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <div class="form-group">
                                            <label class="control-label">尺码类型<span class="must-sign">*</span></label>
                                            <select class="form-control input-w110" id="SizeCodeType">
                                                <option value="">请选择</option>
                                            </select>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="form-group">
                                            <label class="control-label">尺码</label>
                                            <select class="form-control input-w110" id="SizeName">
                                                <option value="">请选择</option>
                                            </select>
                                        </div>
                                    </td>
                                </tr>

                                <tr>
                                    <td colspan="2">
                                        <div class="form-group">
                                            <label class="control-label">数量<span class="must-sign">*</span></label>
                                            <input id="Qty" type="number" name="Qty" placeholder=""
                                                datacol="yes" err="数量" checkexpession="NotNull"
                                                maxlength="3" class="form-control input-w342" />
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
                        <button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/plugins/webuploader/webuploader.min.js"></script>
    <script src="../assets/js/framework.js"></script>
    <script src="../assets/js/page/pageControl.js"></script>
    <script src="../assets/js/plugins/fancybox/jquery.fancybox.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/plugins/bootstrap-typeahead/bootstrap-typeahead.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/img/validate/learunui-validator.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>
    <script type="application/javascript">
        $(document).ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");

            //自动填充(商店)
            $('#shopSearch').typeahead({
                source: function (query, process) {
                    var parameter = { query: query };
                    $('#ShopName').val('');
                    $('#ShopCode').val('');
                    $('#ShopID').val('');
                    Y.API2('DwShop', 'GetShopList', {
                        Keys: $('#shopSearch').val()
                    }, function (data) {
                        if (data.Success == false) {
                            swal(data.Message, '', 'error');
                        } else {
                            var results = data.map(function (item) {
                                return JSON.stringify(item);
                            })
                            process(results);
                        }
                    });
                },
                highlighter: function (item) {
                    var item = JSON.parse(item);
                    return '名称：' + item.ShopName + '　编号：' + item.ShopCode;
                },
                updater: function (item) {
                    var item = JSON.parse(item);
                    $('#ShopName').val(item.ShopName);
                    $('#ShopCode').val(item.ShopCode);
                    $('#ShopID').val(item.ShopID);
                    return item.ShopName;
                }
            });
            //用于记录页数
            var page = 1;
            //用于记录每页多少条记录
            var size =20;

            var isCreate = true;
            shopID = '';
            materialID = '';
            sizeID = '';
            //初始化按钮
            commonUtil.loadBtnList(ModuleID, function () {
                rScroll();
                $('#editBtn').attr('disabled', 'disabled');
                $('#deleteBtn').attr('disabled', 'disabled');
            });

            //加载尺码类型
            commonUtil.getSizeCodeTypeList(["#SizeCodeType"])
            //加载尺码列表
            $(document).on("change", "#SizeCodeType", function () {
                $("#SizeName").html('<option value="">请选择</option>');
                if ($(this).val() == "") {
                    return;
                }
                commonUtil.getSizeList(['#SizeName'], $(this).val());
            });

            //加载信息列表
            var loadList = function () {

                //处理表头
                if ($('.footable thead').children().size() == 0) {
                    tableControl.initThead('.footable',
                    [
                        {
                            trClass: '',
                            children: [
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="checkbox" id="checkAllPlan" />', tdId: '', tdClass: '' },
                                { tdText: '公司代码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                { tdText: '店铺内码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                { tdText: '店铺编号', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                { tdText: '店铺名称', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '货号', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '尺码内码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                { tdText: '尺码名称', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '数量', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' }, ]
                        }
                    ]);
                }

                pageControl.init('PlanOrderQuantity', 'GetList', {
                    keys: $("#search_keys").val(),
                    materialId: $('#search_materialID').val(),
                    Page: page,
                    Size: size,
                    alwaysCallBack: function () {
                        SSID = 0;
                        $('#editBtn').attr('disabled', 'disabled');
                        $('#deleteBtn').attr('disabled', 'disabled');
                    }
                }, function (result) {
                    page = result.page;
                    var tBody = [];
                    if (!Y.checkNull(result.data.Items)) {
                        if (result.data.Items.length > 0) {
                            result.data.Items.map(function (item, i) {
                                var tempTr = {
                                    trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                    children: [
                                       {
                                           tdText: '', c: 0, r: item.length, tdHtml: '<input type="checkbox" class="checkPlan" data-index="' + i + '" />', tdId: '', tdClass: 'diy-td-w46 border-bottom-bold'
                                       },
                                        { tdText: (item.CompanyID || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                        { tdText: (item.ShopID || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'ShopID hide' },
                                        { tdText: (item.ShopCode || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                        { tdText: (item.ShopName || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                        { tdText: (item.MaterialID || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'MaterialID' },
                                        { tdText: (item.SizeID || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'SizeID hide' },
                                        { tdText: (item.SizeName || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                        {
                                            tdText: '', c: 0, r: 0, tdHtml: '<div class="row"> <div class="col-md-12"  style="padding-right: 0px !important;padding-left: 0px  !important;"> <div class="form-group form-group-options"  style="margin-bottom:0px !important"> <div id="3" class="input-group input-group-option quantity-wrapper ">  <span  class="input-group-addon input-group-addon-remove quantity-remove btn btn-danger"> <span class="glyphicon glyphicon-minus"></span> </span>' +
                                                                                       '<input type="number" class="Qty form-control quantity-count" value="' + (item.Qty || '0') +
                                                                                       '" data-ShopID=' + (item.ShopID || '') +
                                                                                       ' data-MaterialID=' + (item.MaterialID || '') +
                                                                                       ' data-SizeID="' + (item.SizeID || '') + '" />' +
                                                                                       '<span class="input-group-addon input-group-addon-remove quantity-add btn btn-primary"> <span class="glyphicon glyphicon-plus"></span> </span>  </div>  </div> </div> </div>', tdId: '', tdClass: 'diy-td-w120'
                                        }
                                    ]
                                };
                                tBody.push(tempTr);
                            })
                        }
                        rScroll();
                    }
                    tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 8, null);
                    //选择全部插件
                    $(".footable").icheckAll({
                        checkObj: ".checkPlan",//表格下面的选择控件对象 选择器
                        checkAllObj: "#checkAllPlan",//全选 选择器
                        clickoutBoxCheck: true,//点击行 不选中
                        checkboxMargin: "4px 0 0 0 "//上右下左
                    });

                });
            }
            loadList();

            //新增按钮
            $('.btns-container').on('click', '#addBtn', function () {
                isCreate = true;
                $('.editTitle').text('新增-企划订单量');
                $('#ShopID').val('');
                $('#ShopCode').val('');
                $('#MaterialID').val('');
                $('#SizeName').val('');
                $('#shopSearch').val('');
                $('#Qty').val('');
            });
            //编辑按钮
            $('.btns-container').on('click', '#editBtn', function () {
                isCreate = false;
                $('.editTitle').text('编辑-企划订单量');
                loadDetail(shopID, materialID, sizeID);
            });
            //查询按钮
            $('.btns-container').on('click', '#btn-search', function () {
                loadList();
            });

            commonUtil.initDeleteBtnEvent({
                callBack: function () {
                    Y.API2('PlanOrderQuantity', 'Remove',
                    {
                        shopID: shopID,
                        materialID: materialID,
                        sizeID: sizeID,
                    }
                        , function (data) {
                            layer.closeAll('dialog');
                            if (data.Success == false) {
                                swal('删除失败', data.Message, 'error');
                            } else {
                                swal('删除成功', '该信息已被你永久删除', 'success');
                                loadList();
                            }
                        });
                }
            });

            var loadDetail = function (shopID, materialID, sizeID) {
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('PlanOrderQuantity', 'GetDetails',
                    {
                        shopID: shopID,
                        materialID: materialID,
                        sizeID: sizeID,
                    }
                    , function (data) {
                        layer.closeAll('dialog');
                        if (data.Success == false) {
                            swal(data.Message, '', 'error');
                        } else {
                            $('#ShopID').val(data.ShopID);
                            $('#shopSearch').val(data.ShopName);
                            $('#ShopCode').val(data.ShopCode);
                            $('#MaterialID').val(data.MaterialID);
                            $('#SizeCodeType').val(data.SizeCodeType);
                            $("#SizeName").html('<option value="">请选择</option>');
                            commonUtil.getSizeList(['#SizeName'], data.SizeCodeType, function () {
                                $('#SizeName').val(sizeID);
                            });
                            $('#Qty').val(data.Qty);
                        }
                    });
            }

            //选中记录
            $('#pageList').on('click', 'tr', function () {
                shopID = $(this).find('.ShopID').text();
                materialID = $(this).find('.MaterialID').text();
                sizeID = $(this).find('.SizeID').text();
                if (shopID.length > 0 && materialID.length > 0 && sizeID.length > 0)
                    $('#editBtn,#deleteBtn').removeAttr('disabled');
                $('.footable tr').removeClass('check');
                $(this).addClass('check');
            });
            /*
            //全选
            $('#checkAll').click(function () {
                if (this.checked)
                    $('.checkItem').each(function () { this.checked = true; });
                else
                    $('.checkItem').each(function () { this.checked = false; });
            });
            */
            //导入
            $(".btns-container").on("click", '#importBtn', function () {
                $.ImportExcelTemplate({
                    filetype: 'PlanOrderQuantity', filename: '企划订单量', action: 'ImportTemplate', after: function () {
                        //回调方法
                        loadList();
                    }
                });
            });

            //保存基本信息(新增或编辑)
            $('#addModuleBtn').click(function () {
                if (!CheckDataValid('#checkModuleForm')) {
                    return false;
                }
                if ($('#shopSearch').val() == '' || $('#shopSearch').val() == '') {
                    swal('输入的店铺名称无效');
                    return false;
                }
                if ($('#GSID').val() == '') {
                    swal('输入的货架无效');
                    return false;
                }
                //检验通过后执行新增或者编辑
                var deal = '';
                var successText = '';
                if (isCreate) {
                    deal = 'Create';
                    successText = '新增成功';
                } else {
                    deal = 'Modify';
                    successText = '编辑成功';
                }
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });

                Y.API2('PlanOrderQuantity', deal, {
                    OldShopID: shopID,
                    OldMaterialID: materialID,
                    OldSizeID: sizeID,
                    ShopID: $('#ShopID').val(),
                    ShopCode: $('#ShopCode').val(),
                    MaterialID: $('#MaterialID').val(),
                    SizeID: $('#SizeName').val(),
                    Qty: $('#Qty').val(),
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal(successText, '', 'success');
                        //关闭新建  or 编辑框
                        $('#closeAddBox').click();
                        //刷新列表
                        loadList();
                    }
                })
            })

            //保存按钮
            $('.btns-container').on('click', '#btn-save', function () {
                if ($('#pageList .checked').size() == 0) {
                    swal('请先勾选要保存的项');
                    return false;
                }
                var submitList = [];
                $('#pageList .checked').each(function () {
                    var a = $(this).parent().parent().find(".Qty");
                    submitList.push({
                        ShopID: a.attr("data-ShopID"),
                        MaterialID: a.attr("data-MaterialID"),
                        SizeID: a.attr("data-SizeID"),
                        Qty: a.val(),
                    })
                });
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('PlanOrderQuantity', 'Save', {
                    list: submitList
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal('保存成功', '', 'success');
                        loadList();
                    }
                })
            })
            //Add
            $("#pageList").on("click", ".quantity-add", function () {
                var count = 0;
                var newcount = 0;
                var countField = $(this).parent().find(".Qty");
                var count = countField.val();
                if (count == '' || parseInt(count) >= 999) {
                    return false;
                }
                else {
                    var newcount = parseInt(count) + 1;
                    countField.val(newcount);
                }

            });
            //Remove
            $("#pageList").on("click", ".quantity-remove", function () {
                var count = 0;
                var newcount = 0;
                var countField = $(this).parent().find(".Qty");
                var count = countField.val();
                if (count == '' || parseInt(count) <= 0) {
                    return false;
                }
                else {
                    var newcount = parseInt(count) - 1;
                    countField.val(newcount);
                }

            });
        })
    </script>
</asp:Content>

